﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Util.Word
{
    public class WordUtils_Aspose
    {
        #region **** Hot Patch 破解方式 **** 

        /// <summary>
        /// 最新版本的破解方法
        /// </summary>
        public static void InitializeAsposeWord()
        {
            InitializeAsposeWords_v18_12_0();
        }

        /// <summary>
        /// Aspose 18.12.0 Hot Patch 破解方式
        /// 
        /// Winform, WPF 请在 OnStartup 中调用此方法
        /// Web程序, 请在全局应用类(Global.asax)中 Application_Start 方法中调用此方法
        /// </summary>
        public static bool InitializeAsposeWords_v18_12_0()
        {
            var name = Assembly.CreateQualifiedName(typeof(Aspose.Words.License).Assembly.FullName,
               "\u000F\u200A\u200B\u2008");
            var licType = Type.GetType(name, false, false);
            var helperName = Assembly.CreateQualifiedName(typeof(Aspose.Words.License).Assembly.FullName,
                "\u000F\u2001\u2000\u2009");
            var helperType = Type.GetType(helperName, false, false);

            if (licType == null || helperType == null)
                return false;
            try
            {
                var helperInstance = helperType.GetField("\u0002", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
                          .GetValue(null);

                helperInstance.GetType().GetField("\u0008", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(helperInstance, 256);

                object lic = Activator.CreateInstance(licType);
                licType.GetField("\u0008\u2000", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
                    .SetValue(lic, (int)1);
                licType.GetField("\u0002\u2000", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
                    .SetValue(lic, DateTime.Today.AddDays(1000));
                licType.GetField("\u000e\u2000", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
                    .SetValue(null, lic);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        #endregion

        public static void String2Word(string saveFilePath, string content)
        {
            Aspose.Words.Document doc = new Aspose.Words.Document();

            var builder = new Aspose.Words.DocumentBuilder(doc);
            // write a new paragraph in the document with the text "Hello World!"
            builder.Writeln(content);

            doc.Save(saveFilePath);
        }

        public static void CreateWordByImages() 
        {
            Aspose.Words.Document doc = new Aspose.Words.Document();

            // doc.

            var builder = new Aspose.Words.DocumentBuilder(doc);
            // write a new paragraph in the document with the text "Hello World!"
            builder.Writeln("Hello World!");

            var fileSavePath = System.IO.Path.Combine
            (
                Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                "testWord"
            );

            doc.Save(fileSavePath);
        }
    }
}
